#include "StdAfx.h"
#include "xhImgAnaly.h"
#include "xkRefPoint.h"
#include "DDFun.h"
#include "xkRefPointMan.h"

#include "DDReadWriteFile.h"

xkRefPoint::xkRefPoint(void)
{
	m_pRefPointMan = NULL;
	m_pImgBit      = NULL;

	Initialize();
}

xkRefPoint::~xkRefPoint(void)
{
	UnInitialize();
}

int xkRefPoint::UnInitialize()
{
	m_pImgBit = NULL;

	return DD_ERR_SUCCESS;
}


int xkRefPoint::Initialize()
{
	xkRefPointHead::m_pAnalyConfig = &m_xAnalyConfig;

	m_xImgBit16.SetImgBit(&m_ImgBitInfo);
	m_xImgBit8.SetImgBit(&m_ImgBitInfo);

	return DD_ERR_SUCCESS;
}

int xkRefPoint::SetImgBuffer(zkImgBitInfo *pImgInfo,BOOL bShareMem)
{
	int nRet = DD_ERR_FAILURE;
	if( NULL == pImgInfo )
	{
		return nRet;
	}

	m_pImgBit = NULL;
	switch( pImgInfo->nImgDefine ) 
	{
	case eImage_Define_Img8:
		{
			m_pImgBit  = &m_xImgBit8;
		}
		break;
	case eImage_Define_Img16:
		{
			m_pImgBit  = &m_xImgBit16;
		}
		break;
	case eImage_Define_Img32:
		{
		}
		break;
	default:
		break;
	}

	do 
	{
		if( NULL == m_pImgBit )
		{
			break;
		}
		nRet = m_pImgBit->SetImgBuffer(pImgInfo,bShareMem);
		if( DD_ERR_SUCCESS != nRet )
		{
			break;;
		}
		
	} while (false);

	return nRet;
}

int xkRefPoint::GetImageSize(int &nWidth,int &nHeight)
{
	int nRet = DD_ERR_FAILURE;
	if( NULL == m_pImgBit )
	{
		return nRet;
	}

	return m_pImgBit->GetImageSize( nWidth,nHeight );
}

int xkRefPoint::GetImgBuffer(zkImgBitInfo **ppImgInfo)
{
    return m_pImgBit->GetImgBuffer( ppImgInfo );
}

int xkRefPoint::GetOrgImgBuffer(zkImgBitInfo **ppImgInfo)
{
	return m_pImgBit->GetOrgImgBuffer( ppImgInfo );
}


int xkRefPoint::SetAnalyCfg(void *pVoid)
{
	if( NULL == pVoid )
	{
		return DD_ERR_PARAMETER;
	}
	if( NULL == m_pImgBit )
	{
		return DD_ERR_FAILURE;
	}

	m_xAnalyConfig = *( (zkAnalyConfig*)pVoid );

#ifdef DKQ_TEST_KEYPOINT_ERROR
	g_xLogInfo.WriteLog(__FILE__,__LINE__,"top:%d,bottom:%d",m_xAnalyConfig.xKeyCfg.rt[0].top,m_xAnalyConfig.xKeyCfg.rt[0].bottom);
#endif
	
// 	if( !CheckKeyRect() )
// 	{
// 		return DD_ERR_ANALY_NOKEYRECT;
// 	}

#ifdef DKQ_TEST_KEYPOINT_ERROR
	g_xLogInfo.WriteLog(__FILE__,__LINE__,"top:%d,bottom:%d",m_xAnalyConfig.xKeyCfg.rt[0].top,m_xAnalyConfig.xKeyCfg.rt[0].bottom);
#endif

	xfRefDetectRect::SetRegion( m_xAnalyConfig.xJudgeCfg );

	return m_pImgBit->CreateKeyPoint( &(m_xAnalyConfig.xKeyCfg) );
}

int xkRefPoint::GetAnalyCfg(void **ppVoid)
{
	*ppVoid = &m_xAnalyConfig;

	return 0;
}

int xkRefPoint::SetUserData(BYTE *pUserData,int nDataSize)
{
	if( NULL == pUserData || nDataSize <= 0 )
	{
		return DD_ERR_PARAMETER;
	}

	m_xUserData.Allocate( nDataSize );

	memcpy( m_xUserData.pBuffer,pUserData,nDataSize );
	m_xUserData.lLength = nDataSize;

	return 0;
}
int xkRefPoint::GetUserData(BYTE **ppUserData,int &nDataSize)
{
	if( m_xUserData.lLength <= 0 )
	{
		return DD_ERR_FAILURE;
	}

	*ppUserData = m_xUserData.pBuffer;
	nDataSize   = m_xUserData.lLength;

	return 0;
}

int xkRefPoint::LoadFile(char *pFileName)
{
	int nRet = -1;

	m_ImgBitInfo.nImgDefine  = eImage_Define_Img16;
	m_ImgBitInfo.bAutoDelete = FALSE;

	do 
	{
		m_pImgBit  = &m_xImgBit16;
		nRet = m_pImgBit->LoadFile(pFileName);
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}


		char *pTmp = NULL;
		char szParamFile[MAX_PATH] = "";
		char szMaskFile[MAX_PATH] = "";
		char szDetRtFile[MAX_PATH] = "";
		char szEdgeFile[MAX_PATH] = "";

		sprintf(szParamFile,pFileName);
		pTmp = strrchr(szParamFile,'.');
		if( NULL != pTmp )
		{
			pTmp[1] = '\0';
		}

		sprintf(szMaskFile,"%s%s",szParamFile,DD_ANYMASKEXT);
		sprintf(szDetRtFile,"%s%s",szParamFile,DD_ANYDETRECTEXT);
#ifdef DD_NEW_EDGE
		sprintf(szEdgeFile,"%s%s.bmp",szParamFile,DD_ANYEDGEEXT);
#else
		sprintf(szEdgeFile,"%s",szParamFile,DD_ANYEDGEEXT);
#endif
		sprintf(szParamFile,"%s%s",szParamFile,DD_ANALYPARAMEXT);


//#define DKQ_TEST_GET_PARAM
#ifdef DKQ_TEST_GET_PARAM
		GetRefDataFromFile( pFileName );
        SaveRefDataToFile( szParamFile ); 
#endif


		//parameter
		if( DDCheckFileExist(szParamFile) )
		{
			nRet = GetRefDataFromFile(szParamFile); 
			if( DD_ERR_SUCCESS != nRet )
			{
				break;
			}
		}

		//mask
		xfRefMask::SetMaskFile( szMaskFile );
		if( DDCheckFileExist(szMaskFile) )
		{
			xfRefMask::LoadMask();
		}
	
		//region
		xfRefDetectRect::SetDetectRegionFile( szDetRtFile );
		if( DDCheckFileExist(szDetRtFile) )
		{
			xfRefDetectRect::LoadDetectRegion();
		}

        //edge
		xkRefPointHead::xfRefEdge::SetEdgeFile( szEdgeFile );
		if( DDCheckFileExist(szEdgeFile) )
		{
			xkRefPointHead::xfRefEdge::LoadEdge();
		}
		

		nRet = m_pImgBit->CreateKeyPoint( &(m_xAnalyConfig.xKeyCfg) );
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}
          
	} while (false);

	return nRet;
}

int xkRefPoint::SaveFile(char *pFileName)
{
	int nRet = -1;

	do 
	{
		if( NULL == m_pImgBit )
		{
			break;
		}
        nRet = m_pImgBit->SaveFile( pFileName );
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}
		m_xPointInfo.nState = eAnalyState_Saved;


		char *pTmp = NULL;
		char szParamFile[MAX_PATH] = "";
		char szMaskFile[MAX_PATH] = "";
		char szDetRtFile[MAX_PATH] = "";
		char szEdgeFile[MAX_PATH] = "";

		sprintf(szParamFile,pFileName);
		pTmp = strrchr(szParamFile,'.');
		if( NULL != pTmp )
		{
			pTmp[1] = '\0';
		}

		sprintf(szMaskFile,"%s%s",szParamFile,DD_ANYMASKEXT);
		sprintf(szDetRtFile,"%s%s",szParamFile,DD_ANYDETRECTEXT);
#ifdef DD_NEW_EDGE
		sprintf(szEdgeFile,"%s%s.bmp",szParamFile,DD_ANYEDGEEXT);
#else
		sprintf(szEdgeFile,"%s",szParamFile,DD_ANYEDGEEXT);
#endif
		sprintf(szParamFile,"%s%s",szParamFile,DD_ANALYPARAMEXT);

		//save parameter 
		if( DDCheckFileExist(szParamFile) )
		{
			::DeleteFile( szParamFile );
		}
		nRet = SaveRefDataToFile( szParamFile );
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

		//mask
		xfRefMask::SetMaskFile( szMaskFile );
		xfRefMask::SaveMask();

		//region
		xfRefDetectRect::SetDetectRegionFile( szDetRtFile );
		xfRefDetectRect::SaveDetectRegion();

		//edge
		xkRefPointHead::xfRefEdge::SetEdgeFile( szEdgeFile );
		

	} while (false);

	return nRet;
}






int xkRefPoint::GetRefDataFromFile(char *pFileName)
{
	int nRet = -1;
	
	DDReadWriteFile xFile;

	do 
	{
		LONGLONG llFileSize = 0;

		if( 0 != xFile.DDCreateFile(pFileName,TRUE,&llFileSize) )
		{
			nRet = -1;
			break;
		}

		LONGLONG llFileOffset = llFileSize - sizeof(zkAnalyFileTail);

		zkAnalyFileTail xFileTail;
		nRet = xFile.DDOperatorFile( (BYTE*)&xFileTail,sizeof(zkAnalyFileTail),&llFileOffset);
		if( nRet != 0)
		{
			break;
		}

		if( !xFileTail.CheckVersion() )
		{
			nRet = -1;
			break;
		}

		DWORD dwRefDataSize = 0;
		//m_xPointInfo
		{
			llFileOffset = xFileTail.dwPointInfoOffset;
			if( xFileTail.dwPointInfoSize != sizeof(m_xPointInfo) )
			{
				nRet = -1;
				break;
			}
			nRet = xFile.DDOperatorFile( (BYTE*)&m_xPointInfo,sizeof(m_xPointInfo),&llFileOffset);
			if( nRet != 0)
			{
				break;
			}
			dwRefDataSize += sizeof(m_xPointInfo);
		}
		//m_xAnalyConfig
		{
			llFileOffset = xFileTail.dwAnalyCfgOffset;
			if( xFileTail.dwAnalyCfgSize != sizeof(m_xAnalyConfig) )
			{
				nRet = -1;
				break;
			}
			nRet = xFile.DDOperatorFile( (BYTE*)&m_xAnalyConfig,sizeof(m_xAnalyConfig),&llFileOffset);
			if( nRet != 0)
			{
				break;
			}
			dwRefDataSize += sizeof(m_xAnalyConfig);
		}
		//m_xUserData
		if( xFileTail.dwUserDataSize > 0 )
		{
			if( !m_xUserData.Allocate( xFileTail.dwUserDataSize ) )
			{
				nRet = DD_ERR_NEWBUFFER;
				break;
			}
			llFileOffset = xFileTail.dwUserDataOffset;
			nRet = xFile.DDOperatorFile( m_xUserData.pBuffer,xFileTail.dwUserDataSize,&llFileOffset);
			if( nRet != 0 )
			{
				break;
			}
			m_xUserData.lLength = xFileTail.dwUserDataSize;
			dwRefDataSize      += xFileTail.dwUserDataSize;

//#define DKQ_TEST_CHANGE_KV
#ifdef DKQ_TEST_CHANGE_KV

#include "DD_PointCfg.h"

			xFile.DDCloseFile();

			DDReadWriteFile xWriteFile;
			if( 0 != xWriteFile.DDCreateFile(pFileName,FALSE,&llFileSize) )
			{
				nRet = -1;
				break;
			}

			zkPointCfg *pPointCfg = (zkPointCfg *)(m_xUserData.pBuffer);

// 			switch( m_xPointInfo.nPoint )
// 			{
// 			case 0:
// 				pPointCfg->xRayCtrl.fKV = 135.0f;
// 				pPointCfg->xRayCtrl.fMA = 3.5f;
// 				break;
// 			case 1:
// 				pPointCfg->xRayCtrl.fKV = 135.0f;
// 				pPointCfg->xRayCtrl.fMA = 3.5f;
// 				break;
// 			case 2:
// 				pPointCfg->xRayCtrl.fKV = 140.0f;
// 				pPointCfg->xRayCtrl.fMA = 3.5f;
// 				break;
// 			case 3:
// 				pPointCfg->xRayCtrl.fKV = 125.0f;
// 				pPointCfg->xRayCtrl.fMA = 3.5f;
// 				break;
// 			case 4:
// 				pPointCfg->xRayCtrl.fKV = 125.0f;
// 				pPointCfg->xRayCtrl.fMA = 3.0f;
// 				break;
// 			case 5:
// 				pPointCfg->xRayCtrl.fKV = 130.0f;
// 				pPointCfg->xRayCtrl.fMA = 3.0f;
// 				break;
// 			}

			pPointCfg->xCapBaseCfg.nIntegralTimes = 16;
			

			llFileOffset = xFileTail.dwUserDataOffset;
			nRet = xWriteFile.DDOperatorFile( m_xUserData.pBuffer,xFileTail.dwUserDataSize,&llFileOffset,FALSE);
			if( nRet != 0 )
			{
				break;
			}
			xWriteFile.DDCloseFile();
#endif
		}
		
		if( dwRefDataSize != xFileTail.dwRefDataSize )
		{
			nRet = -1;
			break;
		}

	} while (FALSE);

	return nRet;
}

int xkRefPoint::SaveRefDataToFile(char *pFileName)
{
	int nRet = -1;

	DDReadWriteFile xFile;

	do 
	{
		LONGLONG llFileSize = 0;

		if( 0 != xFile.DDCreateFile(pFileName,FALSE,&llFileSize) )
		{
			nRet = -1;
			break;
		}
		zkAnalyFileTail xFileTail;
		xFileTail.dwRefDataOffset = (DWORD)llFileSize;

		if( NULL != m_pRefPointMan )
		{
			strcpy(xFileTail.szAnalyTempName,m_pRefPointMan->m_xTempInfo.szTempName);
		}

	
		DWORD    dwRefDataSize = 0;
		LONGLONG llFileOffset  = 0;

		llFileOffset = llFileSize;
		//m_xPointInfo
		{
			xFileTail.dwPointInfoOffset = (DWORD)llFileOffset;
			xFileTail.dwPointInfoSize   = sizeof(m_xPointInfo);
			nRet = xFile.DDOperatorFile( (BYTE*)&m_xPointInfo,sizeof(m_xPointInfo),&llFileOffset,FALSE);
			if( nRet != 0)
			{
				break;
			}
			dwRefDataSize += sizeof(m_xPointInfo);
		}
		//m_xAnalyConfig
		{
			xFileTail.dwAnalyCfgOffset = (DWORD)llFileOffset;
			xFileTail.dwAnalyCfgSize   = sizeof(m_xAnalyConfig);
			nRet = xFile.DDOperatorFile( (BYTE*)&m_xAnalyConfig,sizeof(m_xAnalyConfig),&llFileOffset,FALSE);
			if( nRet != 0)
			{
				break;
			}
			dwRefDataSize += sizeof(m_xAnalyConfig);
		}
		//m_xUserData
		if( m_xUserData.lLength > 0 )
		{
			xFileTail.dwUserDataOffset = (DWORD)llFileOffset;
			xFileTail.dwUserDataSize   = m_xUserData.lLength;
	
			nRet = xFile.DDOperatorFile( m_xUserData.pBuffer,xFileTail.dwUserDataSize,&llFileOffset,FALSE);
			if( nRet != 0 )
			{
				break;
			}
			dwRefDataSize += xFileTail.dwUserDataSize;
		}

		xFileTail.dwRefDataOffset = xFileTail.dwPointInfoOffset;
		xFileTail.dwRefDataSize   = dwRefDataSize;

		nRet = xFile.DDOperatorFile( (BYTE*)&xFileTail,sizeof(zkAnalyFileTail),&llFileOffset,FALSE);
		if( nRet != 0)
		{
			break;
		}

	} while (FALSE);

	return nRet;
}




BOOL xkRefPoint::CheckKeyRect()
{
    zkAnalyKeyPointCfg *pKeyPointCfg = &(m_xAnalyConfig.xKeyCfg);

	RECT *pRt = NULL;

	for(int i=0;i<ANALYKEYRTCOUNT;i++)
	{
        pRt = &( pKeyPointCfg->rt[i] );

		if( pRt->left >= 0 && pRt->left < pRt->right && pRt->top >= 0 && pRt->bottom > pRt->top )
		{
			return TRUE;
		}
	}

	return FALSE;
}



xkRefPointHead *xkRefPoint::GetClosestRefPoint( float *pfThreshold )
{
    return this;
}